/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.BatchEntity;
import entity.DataObjectEntity;
import entity.RawMaterialPurchaseOrderEntity;
import entity.VendorInvoiceEntity;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import type.RawMaterialPurchaseOrderStatus;
import type.VendorInvoiceStatus;

/**
 *
 * @author wei
 */
@Stateless
public class RawMaterialPurchaseOrderEntityFacade extends AbstractFacade<RawMaterialPurchaseOrderEntity> {

    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public RawMaterialPurchaseOrderEntityFacade() {
        super(RawMaterialPurchaseOrderEntity.class);
    }
    
    public List<RawMaterialPurchaseOrderEntity> unfulfilledPurchaseOrders(){
        System.out.println("gonna go into sql");
       Query query = em.createQuery("SELECT p FROM RawMaterialPurchaseOrder p WHERE P.amountLeft>0");
//        System.out.println(query.getResultList().size()+"SIZEE");
//        System.out.println(query.getResultList().get(0));
            return (List<RawMaterialPurchaseOrderEntity>) query.getResultList();
    }

    public List<RawMaterialPurchaseOrderEntity> getOnGoingOrder() {


        String queryStr = "Select r from RawMaterialPurchaseOrder r"
                + " where r.status=:type";
        Query query = em.createQuery(queryStr);
        query.setParameter("type", RawMaterialPurchaseOrderStatus.partlyfilled);
        List<RawMaterialPurchaseOrderEntity> result = query.getResultList();
  
        
        String queryStr2 = "Select r from RawMaterialPurchaseOrder r"
                + " where r.status=:type";
        Query query2 = em.createQuery(queryStr2);
        query2.setParameter("type", RawMaterialPurchaseOrderStatus.fulfilled);
           result.addAll(query2.getResultList());
           
     
 
        return result;
    }
 public List<RawMaterialPurchaseOrderEntity> getPendingOrder1() {


        String queryStr = "Select r from RawMaterialPurchaseOrder r"
                + " where r.status=:type";
        Query query = em.createQuery(queryStr);
        query.setParameter("type", RawMaterialPurchaseOrderStatus.pending);
        return query.getResultList();
 }
    public List<RawMaterialPurchaseOrderEntity> getPendingOrder() {


        String queryStr = "Select r from RawMaterialPurchaseOrder r"
                + " where r.status=:type and r.updated = true";
        Query query = em.createQuery(queryStr);
        query.setParameter("type", RawMaterialPurchaseOrderStatus.pending);
        List<RawMaterialPurchaseOrderEntity> result = query.getResultList();
        String queryStr1 = "Select r from RawMaterialPurchaseOrder r"
                + " where r.status=:type and r.updated = true";
        Query query1 = em.createQuery(queryStr1);
        query1.setParameter("type", RawMaterialPurchaseOrderStatus.partlyfilled);
        
           result.addAll(query1.getResultList());
        String queryStr2 = "Select r from RawMaterialPurchaseOrder r"
                + " where r.status=:type and r.updated = true";
        Query query2 = em.createQuery(queryStr2);
        query2.setParameter("type", RawMaterialPurchaseOrderStatus.fulfilled);


        result.addAll(query2.getResultList());
        return result;
    }

    public List<RawMaterialPurchaseOrderEntity> getFulffiledOrder() {


        String queryStr = "Select r from RawMaterialPurchaseOrder r"
                + " where r.status=:type";
        Query query = em.createQuery(queryStr);
        query.setParameter("type", RawMaterialPurchaseOrderStatus.fulfilled);
        
        return query.getResultList();
    }
        public List<RawMaterialPurchaseOrderEntity> getAlreadyReviewedOrder() {


        String queryStr = "Select r from RawMaterialPurchaseOrder r"
                + " where r.status=:type";
        Query query = em.createQuery(queryStr);
        query.setParameter("type", RawMaterialPurchaseOrderStatus.reviewed);
        
        return query.getResultList();
    }
         public List<RawMaterialPurchaseOrderEntity> getSettledOrder() {


        String queryStr = "Select r from RawMaterialPurchaseOrder r"
                + " where r.status=:type";
        Query query = em.createQuery(queryStr);
        query.setParameter("type", RawMaterialPurchaseOrderStatus.settled);
        
        return query.getResultList();
    }

//    public List<RawMaterialPurchaseOrderEntity> getSettledOrder() {
//
//        List<RawMaterialPurchaseOrderEntity> res =  new ArrayList<RawMaterialPurchaseOrderEntity>();
//System.out.println(res.size()+"tanthanh");
//        for (RawMaterialPurchaseOrderEntity re : res) {
//            if ((!AllInvoiceSettleForThisOrder(re))) {
//                res.remove(re);
//            }
//         
//        }
//        return res;
//    }

    public boolean AllInvoiceSettleForThisOrder(RawMaterialPurchaseOrderEntity re) {
        Collection<VendorInvoiceEntity> v = re.getVendorInvoices();
        if (v.size() == 0) {
            return false;
        }
        for (VendorInvoiceEntity f : v) {
            if (!f.getStatus().equals(VendorInvoiceStatus.settled)) {
                return false;
            }
        }
        return true;
    }

    public List<RawMaterialPurchaseOrderEntity> getOrderMatchInvoice(String name) {

        List<RawMaterialPurchaseOrderEntity> res = new ArrayList<RawMaterialPurchaseOrderEntity>();
        List<RawMaterialPurchaseOrderEntity> list = getOnGoingOrder();

        for (RawMaterialPurchaseOrderEntity r : list) {
            
            if ((r.getRmq().getVendor().getCompanyName().equalsIgnoreCase(name))&& (r.getSettleShip().size()!=0)) {
                res.add(r);
            }
        }
        return res;
    }

    public List<RawMaterialPurchaseOrderEntity> getOrderHaventSettledInvoice() {

        List<RawMaterialPurchaseOrderEntity> temp = getOnGoingOrder();
     
List<RawMaterialPurchaseOrderEntity> result = new ArrayList<RawMaterialPurchaseOrderEntity>();

        for (RawMaterialPurchaseOrderEntity r : temp) {
            Collection<VendorInvoiceEntity> v = r.getVendorInvoices();
            if (v.size() != 0&&(hasOrderInvoicePending(v)) ) {
                result.add(r);
            }
           
        }
        return result;
    }

    public boolean hasOrderInvoicePending(Collection<VendorInvoiceEntity> v) {
        for (VendorInvoiceEntity v1 : v) {
            if (v1.getStatus().equals(VendorInvoiceStatus.pending)) {
                return true;
            }
        }
        return false;
    }

    public Collection<VendorInvoiceEntity> InvoiceHasntRecorded(RawMaterialPurchaseOrderEntity r) {
        Collection<VendorInvoiceEntity> v = r.getVendorInvoices();
        for (VendorInvoiceEntity v1 : v) {
            if (v1.getStatus().equals(VendorInvoiceStatus.pending)) {
                v.remove(v1);
            }
        }
        return v;
    }
}
